package com.aptech.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.aptech.model.Email;
import com.aptech.model.Userprofile;
import com.aptech.session.GetSessionFactory;
import com.opensymphony.xwork2.ActionContext;



public class EmailOperateDao {
	
	//发送邮件
	public void sendEmail(){
		
	}
	
	//打开邮件
	public void OpenEmail(){
		
	}
	
	//查看发件箱
	public List sendedEmail(){
		return null;
	}
	
	//查看收件箱
	public List LookEmail(){
		Session session=GetSessionFactory.getInstance().getSessionFactory().openSession();
		
		//获取session里面的用户名
		String userName=ActionContext.getContext().getSession().get("isLogin").toString();
		
		//查询该用户
		String hql1="from Userprofile as user where user.username=?";
		Query q1=session.createQuery(hql1);
		q1.setParameter(0, userName);
		ArrayList<Userprofile> list=(ArrayList<Userprofile>) q1.list();
		Userprofile user=list.get(0);
		
		//根据用户ID查询有该用户的邮件
		String hql="from Email email,Userprofile user where user=email.userprofileByTouserid and user.id=?";
		try{
			Query q=session.createQuery(hql);
			q.setParameter(0,user.getId());
			ArrayList<?> emails=(ArrayList<?>)q.list();
//			ArrayList<Email> emails=new ArrayList<Email>();
//			for(int i=0;i<l.size();i++){
//				Email email=(Email)l.get(i);
//				emails.add(email);
//			}
			return emails;
		}finally{
			if(session!=null){
				session.close();
			}
		}
	}
	
	//将邮件添加到回收站
	public void recoveryEmail(){
		Session session=GetSessionFactory.getInstance().getSessionFactory().openSession();
		
	}
	
	//删除邮件
	public void deleteEmail(){
		
	}
	
	
	//还原邮件
	public void reductionEmail(){
		
	}
	
	
	//查询所有收件人姓名
	public List selectEmail(){
		Session session=GetSessionFactory.getInstance().getSessionFactory().openSession();
		String sql="selsect username from UserProfile";
		Query q=session.createSQLQuery(sql);
		List l=q.list();
		return l;
	}
}
